package src.com.qf.service.impl;

import src.com.qf.dao.*;
import src.com.qf.dao.impl.*;
import src.com.qf.entity.*;
import src.com.qf.service.OrderService;
import src.com.qf.utils.CodeUtil;
import src.com.qf.utils.JdbcUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao dao = new OrderDaoImpl();
    private GoodCarDao goodCarDao = new GoodCarDaoImpl();
    private OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();
    private AddressDao addressDao = new AddressDaoImpl();
    private GoodDao goodDao = new GoodDaoImpl();
    @Override
    public int addOrder(Order order) {

        try {
            JdbcUtil.begin();
            //向tb_order表中插入数据
            order.setOrderNum(CodeUtil.getOrderNum());
            order.setOrderStatus(0);//默认为0，未付款
            order.setOrderTime(new Date());
            dao.insertOrder(order);

            //往orderDetail表中插入数据
            List<GoodCar> goodCarList = goodCarDao.selectGoodCarsByUid(order.getUid());
            List<OrderDetail> list = new ArrayList<>();
            //将购物车中的信息封装成订单信息
            for (GoodCar car : goodCarList) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setOid(order.getId());//订单id
                orderDetail.setGid(car.getGoodId());
                orderDetail.setNum(car.getGoodNum());
                orderDetail.setSum(car.getGoodSum());
                list.add(orderDetail);
            }

            for (OrderDetail od : list) {
                orderDetailDao.insertOrderDetail(od);
            }
            //清空购物车
            goodCarDao.deleteGoodCarByUid(order.getUid());

            JdbcUtil.commit();
            return 1;
        } catch (Exception e) {
            JdbcUtil.rollback();
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Order> getOrderList(Integer id) {
        List<Order> list = null;
        try {
            JdbcUtil.begin();

            list = dao.selectOrdersByUid(id);
            //由于前端界面需要地址的信息
            for (Order o : list) {
                Address address = addressDao.selectAddressById(o.getAid());
                o.setAddress(address);
            }
            JdbcUtil.commit();
        } catch (SQLException e) {
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Order getOrderDetail(Integer strOid) {
        Order order = null;
        try {
            JdbcUtil.begin();
             order= dao.selectOrdersById(strOid);
            //根据aid得到address对象
            Address address = addressDao.selectAddressById(order.getAid());

            order.setAddress(address);
            //根据oid得到当前有多少个订单
            List<OrderDetail> list =orderDetailDao.selectOrderDetailsByOid(order.getId());
            for (OrderDetail od : list) {
                Good good = goodDao.selectGoodById(od.getGid());
                od.setGood(good);
            }

            order.setOrderDetail(list);

            JdbcUtil.commit();
        } catch (SQLException e) {
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return order;
    }
}
